//
//  ViewController.swift
//  QFIM
//
//  Created by wxc on 2020/10/23.
//

import UIKit

class ViewController: UIViewController , DDTCPClientDelegate{

    var _socket : DDTCPClient?
    var _count : NSInteger?
    var _host : String = "192.168.1.127"
    var _port : UInt16 = 10001
    
    override func viewDidLoad() {
        super.viewDidLoad()

        _socket = DDTCPClient.init()
        _socket?.delegate = self
        _socket?.isDebug = true
        _socket?.reconnectCount = -1
        _socket?.reconnectTimeInterval = 5
        
        _socket?.connectHost(_host, port: _port)
        
//        _socket?.disConnect()
        
        // 登陆
        let register : String = DDTCPClient.convert(toJsonData: [ "password": "123123", "unionId": "777", "userName": "mario" ])
        let registData :Data = register.data(using: .utf8)!
        _socket?.send(registData, typeNum: 01)
        
        // 发消息
        //  * type：消息类型 1 文字 2 图片 3 语音 4 视频
        let message : String = DDTCPClient.convert(toJsonData: [ "fromUnionId": "666", "fromUserName": "wxc", "toUnionId": "777", "message": "你好,在吗", "type": "1", "fileUrl": "" ])
        let messageData :Data = message.data(using: .utf8)!
        _socket?.send(messageData, typeNum: 03)
        
        
        DispatchQueue.main.async {
            let vc = ConversationViewController.init()
            self.present(vc, animated: true) {
                
            }
        }
        
    }


    func client(_ client: DDTCPClient!, didReadData dic: [AnyHashable : Any]!) {
        print("收到消息\(String(describing: dic))")
    }
    
    func client(_ client: DDTCPClient!, didConnect host: String!, port: UInt16) {
        print("链接成功")
        _socket?.heartData = Data.init()
    }
    
    func clientDidDisconnect(_ client: DDTCPClient!) {
        print("clientDidDisconnect")
    }
    
    
    
}

extension ViewController {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            }
}


/*

 命令 :
        消息              3
        群消息            21
        推送              19
        心跳              17
        登陆              01
        登陆返回数据        02

 测试包
 12345678010101000000257b2270617373776f7264223a22313233313233222c22756e696f6e4964223a22363636227d

 测试包分解

 12345678  head
 01        version
 01        序列化算法
 01        命令
 00000025  长度（int）

 7b2270617373776f7264223a22313233313233222c22756e696f6e4964223a22363636227d  实体类二进制

 {"password":"123123","unionId":"666"}



public class {
     private String unionId;

     private String userName;

     private boolean success;

     private String reason;
}

  * fromUnionId 发送用户唯一标识
  * fromUserName 发送用户唯一名称
  * toUnionId   接收用户唯一标识
  * message  发送消息
  * fileUrl 附加文件地址
  * type：消息类型 1 文字 2 图片 3 语音 4 视频

 public class MessageRequestPacket extends Packet {
     private String fromUnionId;
     private String fromUserName;
     private String toUnionId;
     private String message;
     private String fileUrl;
     private int type;
 */


